package com.power.sifa.approve.model.doc;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.plf.core.base.model.EnumerationHelper;
import com.power.sifa.approve.dao.Apl_applicantDao;
import com.power.sifa.approve.dao.Apl_thirdperDao;
import com.power.sifa.approve.model.Apl_applicant;
import com.power.sifa.approve.model.Apl_thirdper;
import com.power.sifa.approve.model.enumeration.Tp_applicant;
import com.power.sifa.approve.model.enumeration.Tp_procmaterial;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  行政复议终止决定书
 */
@Data
public class HearAuditTerminDoc extends AbstractDoc{

    /**
     * 所有人员信息
     */
    private String applystr;
    /**
     * 正文
     */
    private String content;


    private final static String TEMPLATE = "    申请人对被申请人{behavior}不服提出的行政复议申请，本府依法已予受理。行政复议期间，（终止审理的事由）。" +
            "根据《中华人民共和国行政复议法》或者《中华人民共和国行政复议法实施条例》第四十二条第（xxx）款第（xxx）项的规定，行政复议终止";


    public HearAuditTerminDoc() {
        super();
        this.setDocType(Tp_procmaterial.HEAR_TERMIN);
    }

    public HearAuditTerminDoc(String applyid) {
        super(applyid);
        this.setDocType(Tp_procmaterial.HEAR_TERMIN);
    }

    public HearAuditTerminDoc(String applyid, String applystr, String content) {
        super(applyid);
        this.setDocType(Tp_procmaterial.HEAR_TERMIN);
        this.applystr = applystr;
        this.content = content;
    }

    @Override
    public AbstractDoc generateTemplate(String applyid) {
        HearAuditTerminDoc template = new HearAuditTerminDoc(applyid);
        // 案号
        template.setApl_admre_audit_code(this.getApl_admre_audit_code());
        String applyStr = this.getApplystr(applyid);
        template.setApplystr(applyStr);
        String content = TEMPLATE.replace("{behavior}", this.transBehavior());
        template.setContent(content);
        return template;
    }


    private String getApplystr(String applyid) {
        EnumerationHelper helper =  SpringUtil.getBean(EnumerationHelper.class);
        Apl_applicantDao apl_applicantDao =  SpringUtil.getBean(Apl_applicantDao.class);
        Apl_thirdperDao apl_thirdperDao =  SpringUtil.getBean(Apl_thirdperDao.class);
        List<Apl_applicant> applicantList = apl_applicantDao.getByApplyid(applyid);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatT = new SimpleDateFormat("yyyy年M月d日");
        StringBuffer applystr = new StringBuffer();
        // 申请人
        {
            List<Apl_applicant> applist = applicantList.stream().filter(p -> Tp_applicant.CITIZEN == p.getType()).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(applist)) {
                applystr.append("    ");
                for (Apl_applicant v : applist) {
                    applystr.append("申请人：");
                    applystr.append(v.getName());
                    if (null != v.getGender()) {
                        applystr.append("，" + helper.getDescription(v.getGender()));
                    }
                    if (null != v.getNation()) {
                        applystr.append("，" + helper.getDescription(v.getNation()));
                    }
                    if (StringUtils.isNotEmpty(v.getBirthym())) {
                        try {
                            applystr.append("，" + formatT.format(format.parse(v.getBirthym())));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    if (null != v.getAddr()) {
                        applystr.append("，" + v.getAddr());
                    }
                    applystr.append("\n");
                }
            }
        }
        // 法人或者其他组织
        {
            List<Apl_applicant> aplentlist = applicantList.stream().filter(p -> Tp_applicant.UNIT == p.getType()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(aplentlist)) {
                applystr.append("    ");
                for (Apl_applicant v : aplentlist) {
                    applystr.append("申请人（法人或者其他组织）：");
                    applystr.append(v.getName());
                    if(null != v.getAddr()) {
                        applystr.append("，" + v.getAddr());
                        applystr.append("\n");
                    }
                    if(null != v.getManager()) {
                        applystr.append("    ");
                        applystr.append("法定代表人或者主要负责人:");
                        applystr.append("，" + v.getManager());
                        if(null != v.getJob()) {
                            applystr.append("，" + v.getJob());
                        }
                        applystr.append("\n");
                    }
                }
            }
        }
        // 被申请人
        applystr.append("    ").append("被申请人：");
        applystr.append(this.getAdmreObj().getTargetname());
        applystr.append("\n");
        // 第三人
        {
            List<Apl_thirdper> thilist = apl_thirdperDao.getByApplyid(applyid);
            if(!CollectionUtils.isEmpty(thilist)) {
                applystr.append("    ");
                for (Apl_thirdper v : thilist) {
                    applystr.append("第三人：");
                    applystr.append(v.getName());
                    if(null != v.getAddr()) {
                        applystr.append("，" + v.getAddr());
                    }
                    applystr.append("\n");
                }
            }
        }
        return  applystr.toString();
    }
}
